const express=require('express');
const home=express.Router();
const axios = require('axios')
const jwt = require("jsonwebtoken")//解析token
home.get('/',(req, res)=>{
    // console.log(222);
    // 嵌套axios嵌套axios
    // 得到首页所有商品
    // console.log(req.session.token);
    let goodsData
    axios({
        url:'http://www.mianded.top:8081/goods/getAllHome',
        method:'GET',
        headers:{
            token:req.session.token?req.session.token:"",
        }
    })
    .then(data => {
        goodsData = data.data.data[0]
        // console.log(goodsData);
        axios({
            url:'http://www.mianded.top:8081/goods/getAllHomeType',
            method:'GET',
            headers:{
                token:req.session.token?req.session.token:"",
            }
        })
        .then(data => {
            cateData= data.data.data
            // console.log(cateData.length);
            // console.log(goodsData[0].length);
            // console.log(cateData);
            // console.log(goodsData);
            const arr = []
            cateData.forEach((item, index) => {
                // console.log(item,index);
                const {homeType} = item
                // console.log(goodsData[index]);
                // if (!goodsData[index].length) return
                let obj = {
                    homeType,
                    data: goodsData[index]
                }                                                                                                                                                                                                
                arr.push(obj)
            });
            // console.log(arr);
            let flagLogin
            if(req.session.token === null || !req.session.token){
                flagLogin = 0
            }else{
                flagLogin = 1
            }
            let userInfoL = {
                flagLogin,
                userInfo:jwt.decode(req.session.token)
            }
            console.log(jwt.decode(req.session.token));
            console.log(userInfoL);
            res.render('home/index',{
                arr:arr,
                userInfoL:userInfoL
            })
        })
        .catch(err => {
            console.log(err);
        })
        // res.render('home/index',{goodsData})
            })
    .catch(err => {
        // console.log(41);
        console.log(err);
    })
});
const index = require("./home/index")
const register = require("./home/register")
const login = require("./home/login")
const find = require("./home/find")
const center = require("./home/center")
const order = require("./home/order")
const orderNo = require("./home/orderNo")
const footmark = require("./home/footmark")
const address = require("./home/address")
const collection = require("./home/collection")
const shopstage = require("./home/shopstage");
const add = require("./home/add");
const safe = require("./home/safe");
const whoareyou = require("./home/whoareyou");
const shopreg = require("./home/shopreg");

const expressmessage = require("./home/expressmessage");
const refuseExpress = require("./home/refuseExpress");
const productDetail = require("./home/productDetail")
const shoppingCart = require("./home/shoppingCart")
const search = require("./home/search")
const creatOrder = require("./home/creatOrder")
const userComment = require("./home/userComment");
const searchShop = require("./home/searchShop")
const { userInfo } = require('os');


home.use('/index',index)
home.use('/register',register)
home.use('/login',login)
home.use('/find',find)
home.use('/center',center)
home.use('/order',order)
home.use('/order',orderNo)
home.use('/footmark',footmark)
home.use('/address',address)
home.use('/collection',collection)
home.use('/shopstage',shopstage)
home.use('/add',add)
home.use('/whoareyou',whoareyou)
home.use('/shopreg',shopreg)

home.use('/safe',safe)
home.use('/expressmessage',expressmessage)
home.use('/refuseExpress',refuseExpress)
// home.use('/search',search)
home.use('/search',search)
home.use('/productDetail',productDetail)
home.use('/shoppingCart', shoppingCart)
home.use('/creatOrder', creatOrder)
home.use('/userComment', userComment)
home.use('/searchShop', searchShop)


home.get('/login',(req,res)=>{
    res.render('home/login')
});
home.get('/register',(req,res)=>{
    res.render('home/register')
});
home.get('/find',(req,res)=>{
    
    res.render('home/find')
});
home.get('/searchShop', (req, res)=> {
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    // console.log(jwt.decode(req.session.token));
    // console.log(userInfoL);
    res.render('home/searchShop',{
        userInfoL:userInfoL
    })
    // res.render('home/searchShop')
})
home.get('/center',(req,res)=>{
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    if(jwt.decode(req.session.token).sex == "男"){
        sex = 1
    }else{
        sex = 0
    }
    let userInfoL = {
        flagLogin,
        sex,
        userInfo:jwt.decode(req.session.token)
    }
    res.render('home/center',{
        userInfoL:userInfoL,
    })
    // res.render('home/center')
});
home.get('/order',(req,res)=>{
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    // console.log(jwt.decode(req.session.token));
    // console.log(userInfoL);
    res.render('home/order',{
        userInfoL:userInfoL
    })
    // res.render('home/order')
});
home.get('/orderNo',(req,res)=>{
    const {shopId, goodsId, color, size, count} = req.query
    let orderInfo ={
        shopId:shopId,
        goodsId:goodsId,
        color:color,
        size:size,
        count:count
    }
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    // console.log(jwt.decode(req.session.token));
    // console.log(userInfoL);
    res.render('home/orderNo',{
        userInfoL:userInfoL,
        orderInfo:orderInfo
    })
    // res.render('home/orderNo')
});
home.get('/footmark',(req,res)=>{
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    // console.log(jwt.decode(req.session.token));
    // console.log(userInfoL);
    res.render('home/footmark',{
        userInfoL:userInfoL
    })
    // res.render('home/footmark')
});
home.get('/address',(req,res)=>{
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    // console.log(jwt.decode(req.session.token));
    // console.log(userInfoL);
    res.render('home/address',{
        userInfoL:userInfoL
    })
    // res.render('home/address')
});
home.get('/collection',(req,res)=>{
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    // console.log(jwt.decode(req.session.token));
    // console.log(userInfoL);
    res.render('home/collection',{
        userInfoL:userInfoL
    })
    // res.render('home/collection')
});
home.get('/shopstage',(req,res)=>{
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    // console.log(jwt.decode(req.session.token));
    // console.log(userInfoL);
    res.render('home/shopstage',{
        userInfoL:userInfoL
    })
    // res.render('home/shopstage')
});
home.get('/add',(req,res)=>{
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    // console.log(jwt.decode(req.session.token));
    // console.log(userInfoL);
    res.render('home/add',{
        userInfoL:userInfoL
    })
    // res.render('home/add')
});
home.get('/safe',(req,res)=>{
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    // console.log(jwt.decode(req.session.token));
    // console.log(userInfoL);
    res.render('home/safe',{
        userInfoL:userInfoL
    })
    // res.render('home/safe')
});
home.get('/whoareyou',(req,res)=>{
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    // console.log(jwt.decode(req.session.token));
    // console.log(userInfoL);
    res.render('home/whoareyou',{
        userInfoL:userInfoL
    })
    // res.render('home/safe')
});
home.get('/shopreg',(req,res)=>{
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    // console.log(jwt.decode(req.session.token));
    // console.log(userInfoL);
    res.render('home/shopreg',{
        userInfoL:userInfoL
    })
    // res.render('home/safe')
});
home.get('/expressmessage',(req,res)=>{
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    // console.log(jwt.decode(req.session.token));
    // console.log(userInfoL);
    res.render('home/expressmessage',{
        userInfoL:userInfoL
    })
    // res.render('home/expressmessage')
});
home.get('/refuseExpress',(req,res)=>{
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    // console.log(jwt.decode(req.session.token));
    // console.log(userInfoL);
    res.render('home/refuseExpress',{
        userInfoL:userInfoL
    })
    // res.render('home/refuseExpress')
});
home.get('/productDetail', (req, res) =>{
    // let {url} = req
    // let index = url.indexOf('?')+1
    // url = url.substring(index)
    // let arr = url.split('&')
    // let obj = {}
    // for(let i = 0; i<arr.length; i++){
    //     let str1 = arr[i].split('=')[0]
    //     let str2 = arr[i].split('=')[1]
    //     obj[str1] = str2
    // }
    // console.log(obj);
    
    // axios({
    //     url:'http://www.mianded.top:8081/inGoodsPage',
    //     method:'POST',
    //     params:{
    //         shopId:obj.shopId
    //     },
    //     headers:{
    //         token:req.session.token?req.session.token:"",
    //     }
    // })
    // .then(data => {
    //     let flagLogin
    
    // })
    // .catch(err => {
    //     console.log(err);
    // })
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    // console.log(jwt.decode(req.session.token));
    // console.log(userInfoL);
    res.render('home/productDetail',{
        userInfoL:userInfoL
    })
    // res.render('home/productDetail')
})
home.get('/shoppingCart', (req, res) => {
    // if((req.session.token === null)||(!req.session.token)){
    //     res.redirect('/home/login')
    // }else{
    //     console.log(170);
    //     console.log(jwt.decode(req.session.token));
    //     res.render('home/shoppingCart', {userInfo: jwt.decode(req.session.token)})
    // }
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    // console.log(jwt.decode(req.session.token));
    // console.log(userInfoL);
    res.render('home/shoppingCart',{
        userInfoL:userInfoL
    })
})
home.get('/search', (req, res)=>{
    // let {url} = req
    // let index = url.indexOf('?')+1
    // url = url.substring(index)
    // let arr = url.split('&')
    // let obj = {}
    // for(let i = 0; i<arr.length; i++){
    //     let str1 = arr[i].split('=')[0]
    //     let str2 = arr[i].split('=')[1]
    //     obj[str1] = str2
    // }
    // console.log(obj);
    // let content = url.split('=')[1]
    // let setting = url.split('=')[2]
    // console.log(content)
    // console.log(setting);
    // content:encodeURIComponent(content),
    // axios({
    //     url:'http://www.mianded.top/getAllLikeHome',
    //     method:'POST',
    //     params:{
    //         content:encodeURIComponent(obj.content),
    //         setting:obj.setting
    //     }
    //     // headers:{
    //     //     token:req.session.token?req.session.token:"",
    //     //     'Content-Type':'application/x-www-form-urlencoded'
    //     // }
    // })
    // .then(data =>{
    //     console.log('+++++++++++++++++++++++++++');
    //     console.log(data);
    //     // let info = data.data[0]
    //     // res.render('home/search', {info})
    // })
    // .catch(err => {
    //     console.log(err);
    //     // 返回一个错误页面
    // })
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    // console.log(jwt.decode(req.session.token));
    // console.log(userInfoL);
    res.render('home/search',{
        userInfoL:userInfoL
    })
    // res.render('home/search')
    
})
home.get('/productDetail/creatOrder', (req, res) => {
    const {shopId, goodsId, color, size, count} = req.query
    // console.log(req.body);
    // console.log(req.query);
    console.log(color);
    console.log(size);
    let goodsInfo = {
        shopId,
        goodsId,
        color,
        size,
        count
    }
    console.log(goodsInfo);
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    res.render('home/creatOrder',{
        userInfoL:userInfoL,
        goodsInfo:goodsInfo

    })
    // res.render('home/creatOrder')
})
home.get('/userComment', (req, res) => {
    let flagLogin
    if(req.session.token === null || !req.session.token){
        flagLogin = 0
    }else{
        flagLogin = 1
    }
    let userInfoL = {
        flagLogin,
        userInfo:jwt.decode(req.session.token)
    }
    // console.log(jwt.decode(req.session.token));
    // console.log(userInfoL);
    res.render('home/userComment',{
        userInfoL:userInfoL
    })
    // res.render('home/userComment')
})


module.exports=home;